//
//  DSLeaderboardViewModel.m
//  DSJY
//
//  Created by 冯华强 on 16/9/28.
//  Copyright © 2016年 dsjy. All rights reserved.
//  排行榜VM

#import "DSLeaderboardViewModel.h"
#import "DSLearnInfoViewModel.h"
#import "DSNetworkTool.h"
#import "DSAccountViewModel.h"
#import "DSLearnInfo.h"
#import "DSLearnStatis.h"

@interface DSLeaderboardViewModel ()
//模型
@property (nonatomic, strong, readwrite) NSMutableArray<DSLearnInfoViewModel *> *learnInfoVMList;
//我的成绩
@property (nonatomic, strong, readwrite) DSLearnStatis *myLearnStatis;
//我的本月成绩
@property (nonatomic, strong, readwrite) DSLearnInfo *myMonthLearnInfo;
@end

@implementation DSLeaderboardViewModel

#pragma mark - 懒加载
- (NSMutableArray<DSLearnInfoViewModel *> *)learnInfoVMList
{
    if (_learnInfoVMList == nil) {
        _learnInfoVMList = [NSMutableArray<DSLearnInfoViewModel *> array];
    }
    return _learnInfoVMList;
}

#pragma mark - 初始化
- (instancetype)init
{
    if (self = [super init]) {
        //绑定事件
        [self initialBind];
    }
    return self;
}

- (void)initialBind
{
    @weakify(self);
    //初始化数据
    self.requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        self.pageNum = [input isEqual:@(YQRefreshTypeFooter)] ? self.pageNum+1 : 1;
        RACSignal *toDaySignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [DSNetworkTool toDayHeroesListWithParams:[DSAccountViewModel sharedAccount].account pagenum:self.pageNum pagecount:self.pageCount success:^(NSDictionary *responseObject) {
                //返回值
                YQResponse<DSLearnInfo *> *response = [YQResponse mj_objectWithKeyValues:responseObject];
                [self veriResponse:response finishedCallBack:^{
                    //列表
                    NSArray<DSLearnInfo *> *learnInfoList = [DSLearnInfo mj_objectArrayWithKeyValuesArray:responseObject[@"datas"]];
                    response.dataList = learnInfoList;
                    
                    //我的成绩
                    self.myLearnStatis = [DSLearnStatis mj_objectWithKeyValues:responseObject[@"attrs"]];
                    
                    [subscriber sendNext:response];
                }];
                [subscriber sendCompleted];
            } failure:^(NSError *error) {
                MTLog(@"%@", error);
                [SVProgressHUD showErrorWithStatus:@"系统异常!"];
                [subscriber sendNext:error];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
        
        RACSignal *monthSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [DSNetworkTool monthHeroesListWithParams:[DSAccountViewModel sharedAccount].account pagenum:self.pageNum pagecount:self.pageCount success:^(NSDictionary *responseObject) {
                //返回值
                YQResponse<DSLearnInfo *> *response = [YQResponse mj_objectWithKeyValues:responseObject];
                [self veriResponse:response finishedCallBack:^{
                    //列表
                    NSArray<DSLearnInfo *> *learnInfoList = [DSLearnInfo mj_objectArrayWithKeyValuesArray:responseObject[@"datas"]];
                    response.dataList = learnInfoList;
                    
                    //我的成绩
                    self.myMonthLearnInfo = [DSLearnInfo mj_objectWithKeyValues:responseObject[@"attrs"]];
                    
                    [subscriber sendNext:response];
                }];
                [subscriber sendCompleted];
            } failure:^(NSError *error) {
                MTLog(@"%@", error);
                [SVProgressHUD showErrorWithStatus:@"系统异常!"];
                [subscriber sendNext:error];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
        
        return [self rac_liftSelector:@selector(updateUIWithToDayList:monthList:) withSignalsFromArray:@[toDaySignal, monthSignal]];
    }];
    
    //设置请求时的提示框
    [self executingWithCommand:self.requestCommand];
}

#pragma mark - 设置事件
/**
 *  更新UI
 */
- (void)updateUIWithToDayList:(id)toDayResp monthList:(id)monthResp
{
    if ([toDayResp isKindOfClass:[NSError class]] || [monthResp isKindOfClass:[NSError class]]) {
        return;
    }
    
    YQResponse<DSLearnInfo *> *newToDayResp = toDayResp;
    YQResponse<DSLearnInfo *> *newMonthResp = monthResp;
    if (newToDayResp.total_page >= newMonthResp.total_page) {
        self.response = newToDayResp;
    }else{
        self.response = newMonthResp;
    }
    
    NSArray<DSLearnInfo *> *newToDayList = newToDayResp.dataList;
    NSArray<DSLearnInfo *> *newMonthList = newMonthResp.dataList;

    //没有分页后不需要这个操作
//    if(newToDayResp.total_page < self.pageNum){
//        newToDayList = @[];
//    }
//    
//    if(newMonthResp.total_page < self.pageNum){
//        newMonthList = @[];
//    }
    
    if (self.pageNum == 1){
        [self.learnInfoVMList removeAllObjects];
    }
    
    NSMutableArray<DSLearnInfoViewModel *> *addlearnInfoVMList = [NSMutableArray<DSLearnInfoViewModel *> array];
    
    NSInteger count = newMonthList.count;//MAX(newToDayList.count, newMonthList.count);
    for (NSInteger i = 0; i < count; i++) {
        DSLearnInfoViewModel *learnInfoVM = [[DSLearnInfoViewModel alloc] init];
        [addlearnInfoVMList addObject:learnInfoVM];
    }
    
    if (newMonthList != nil && newMonthList.count != 0) {
        NSInteger index = -1;
        for (NSInteger i = 0; i < newMonthList.count; i++) {
            DSLearnInfoViewModel *learnInfoVM = addlearnInfoVMList[i];
            DSLearnInfo *monthLearnInfo = newMonthList[i];
            learnInfoVM.monthLearnInfo = monthLearnInfo;
            
            //如果学生id相等的
            if ([monthLearnInfo.studentid isEqualToString:self.myMonthLearnInfo.studentid]) {
                index = i;
            }
        }
        
        //不管什么情况都添加...
        [addlearnInfoVMList addObject:[self createEmptyLearnInfoVM]];
        if (index == -1 && self.myMonthLearnInfo.studentid != nil) {  //代表不在列表中
            //添加我的本月成绩
            DSLearnInfoViewModel *learnInfoVM = [[DSLearnInfoViewModel alloc] init];
            learnInfoVM.monthLearnInfo = self.myMonthLearnInfo;
            [addlearnInfoVMList addObject:learnInfoVM];
            
            //添加@"......"
            [addlearnInfoVMList addObject:[self createEmptyLearnInfoVM]];
        }
    }
    
    count = newToDayList.count - addlearnInfoVMList.count;
    if (count > 0) {
        for (NSInteger i = 0; i < count; i++) {
            DSLearnInfoViewModel *learnInfoVM = [[DSLearnInfoViewModel alloc] init];
            [addlearnInfoVMList addObject:learnInfoVM];
        }
    }
    
    if (newToDayList != nil && newToDayList.count != 0) {
        for (NSInteger i = 0; i < newToDayList.count; i++) {
            DSLearnInfoViewModel *learnInfoVM = addlearnInfoVMList[i];
            learnInfoVM.todayLearnInfo = newToDayList[i];
        }
    }
    
    [self.learnInfoVMList addObjectsFromArray:addlearnInfoVMList];
}

- (DSLearnInfoViewModel *)createEmptyLearnInfoVM
{
    DSLearnInfoViewModel *learnInfoVM = [[DSLearnInfoViewModel alloc] init];
    DSLearnInfo *monthLearnInfo = [[DSLearnInfo alloc] init];
    monthLearnInfo.score = @"";
    monthLearnInfo.studentname = @"......";
    monthLearnInfo.paiming = @"";
    monthLearnInfo.isEmpty = YES;
    learnInfoVM.monthLearnInfo = monthLearnInfo;
    return learnInfoVM;
}

@end
